package org.springblade.tms.config;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.TenantConstant;
import org.springblade.system.feign.IDictBizClient;
import org.springblade.tms.global.constant.GPSConSigName;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;

import static org.springblade.tms.global.constant.DictCodeConstant.CONSIG_NAME;
import static org.springblade.tms.global.constant.DictCodeConstant.THIRD_INTERFACE;

/**
 * @author: wzq
 * @className: AppInit
 * @createDate: 2022/3/6
 * @description: 启动后初始化逻辑
 */
@Slf4j
@Component
public class AppInit implements ApplicationRunner {

	@Value("${server.port}")
	private String port;
	@Resource
	private IDictBizClient dictBizClient;

	@Override
	public void run(ApplicationArguments args) {
		log.info("/****************************gen swagger code start****************************/");
		try {
			String url = "http://localhost:" + port + "/v2/api-docs";
			String file = "blade-service/blade-tms/doc/swagger.json";
			HttpUtil.downloadFile(url, new File(file));
		} catch (Exception e) {
			log.error("/****************************error****************************/", e);
		}
		log.info("/****************************gen swagger code end****************************/");
		System.out.println("/****************************发布成功****************************/");
		String data = dictBizClient.getValue(THIRD_INTERFACE, CONSIG_NAME, TenantConstant.DEFAULT_TENANT_ID).getData();
		log.info("========CONSIG_NAME++++++" + data + "==================");
		GPSConSigName.CONSIG_NAME = data;
	}
}
